package cn.lsh.reducer;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import java.io.IOException;

public class HbaseReduce extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {


	@Override
	protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
		int sum = 0;
		for (IntWritable intWritable : values) {
			sum += intWritable.get();
		}
		Put put = new Put(Bytes.toBytes(key.toString()));
		//这里value以String类型序列化，便于在shell查看数据
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("count"), Bytes.toBytes(sum + ""));
		context.write(null, put);
	}
}
